package com.service.quartz.listener;

import com.service.quartz.constant.TaskConstant;
import com.service.quartz.event.CreateJobEvent;
import com.service.quartz.manager.JobManage;
import com.service.quartz.model.TaskModel;
import com.service.quartz.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CreateJobEventListener {
    @Autowired
    private JobManage jobManage;

    @EventListener
    public void createJobEvent(CreateJobEvent event) {
        TaskModel taskModel = event.getTaskModel();
        jobManage.addJob(
                taskModel.getId(),
                String.valueOf(taskModel.getId()),
                TaskConstant.TASK_GROUP_NAME,
                String.valueOf(taskModel.getId()),
                TaskConstant.TASK_GROUP_NAME,
                TaskService.class,
                taskModel.getCronTab());
        log.warn("添加任务 {}", taskModel);
    }
}
